package com.admin.business.voteUserToken;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.admin.utils.RedisUtilKey;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import java.util.List;

/**
 * @description: token表
 * @create: 2023-10-24 14:41:35
 **/
@Service("voteUserTokenService")
@Slf4j
public class VoteUserTokenService{

    @Resource
    VoteUserTokenDao voteUserTokenDao;

    @Autowired
    private RedisUtilKey redisTemplate;


    /**
     * 用户登录删除之前登录的信息从新生成
     * @param cdoRequest
     * @return
     */
    @DS("DS")
    @OpenApi
    public Return addUpdateUserToken(CDO cdoRequest) {
        //查询之前所以信息信息
        CDO userToken=new CDO();
        userToken.set("user_id",cdoRequest.getString("user_id"));
        userToken.set("user_id_zyw",cdoRequest.getString("user_id_zyw"));
        List<CDO> vutp = voteUserTokenDao.getVoteUserTokenListPage(userToken);

        //删除信息和之前存储redis信息
        for (CDO cdo : vutp) {
            voteUserTokenDao.deleteVoteUserTokenById(cdo.getInteger("id"));
            redisTemplate.del(cdo.getString("token_value"));
        }
        //重新生成一条
        voteUserTokenDao.addVoteUserToken(cdoRequest);
        return Return.valueOf(cdoRequest);
    }



    /**
     * 添加token表,获取id数据
     *
     * @param cdoRequest - id:
     *                   - user_id:用户id
     *                   - user_id_zyw:用户id
     *                   - token_value:token值
     *                   - token_state:token状态：0有效1失效
     *                   - effective_time:有效时间
     *                   - update_time:更新时间
     *                   - begin_time:创建时间
     *                   - state:删除，0正常1删除
     * @return
     * @author wdf
     * @create: 2023-10-24 14:41:35
     */
    @OpenApi
    public Return addVoteUserToken(CDO cdoRequest) {
        voteUserTokenDao.addVoteUserToken(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("id")) {
            cdoResult.set("id", cdoRequest.get("id"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，更新token表数据
     *
     * @param cdoRequest - id:
     *                   - user_id:用户id
     *                   - user_id_zyw:用户id
     *                   - token_value:token值
     *                   - token_state:token状态：0有效1失效
     *                   - effective_time:有效时间
     *                   - update_time:更新时间
     *                   - begin_time:创建时间
     *                   - state:删除，0正常1删除
     * @return
     * @author wdf
     * @create: 2023-10-24 14:41:35
     */
    @OpenApi
    public Return updateVoteUserTokenById(CDO cdoRequest) {
        voteUserTokenDao.updateVoteUserTokenById(cdoRequest);
        return Return.SUCCESS;
    }

    /**
     * 根据id，删除token表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author wdf
     * @create: 2023-10-24 14:41:35
     */
    @OpenApi
    public Return deleteVoteUserTokenById(CDO cdoRequest){
        int count = voteUserTokenDao.deleteVoteUserTokenById(cdoRequest.getInteger("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("count", count);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除token表数据
     *
     * @param cdoRequest
     * @return
     * @author wdf
     * @create: 2023-10-24 14:41:35
     */
    @OpenApi
    public Return deleteVoteUserToken(CDO cdoRequest) {
        int count = voteUserTokenDao.deleteVoteUserToken(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("count", count);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取token表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author wdf
     * @create: 2023-10-24 14:41:35
     */
    @OpenApi
    public Return getVoteUserTokenById(CDO cdoRequest) {
        CDO cdoMapperResult = voteUserTokenDao.getVoteUserTokenById(cdoRequest.getInteger("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取token表数据列表
     *
     * @param cdoRequest
     * @return
     * @author wdf
     * @create: 2023-10-24 14:41:35
     */
    @OpenApi
    public Return getVoteUserTokenListPage(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.getInteger("pageIndex")-1;
        int recordSize = 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteUserTokenDao.getVoteUserTokenListPage(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteUserTokenDao.getVoteUserTokenCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * token校验获取一条记录
     *
     * @param cdoRequest
     * @return
     * @author wdf
     * @create: 2023-10-24 14:41:35
     */
    @OpenApi
    public Return getVoteUserTokenPage(CDO cdoRequest) {
        CDO cdoMapperResultList = voteUserTokenDao.getVoteUserTokenPage(cdoRequest);
        return Return.valueOf(cdoMapperResultList);

    }

    /**
     * 根据混合条件，获取token表数据列表
     *
     * @param cdoRequest - id:
     * @return
     * @author wdf
     * @create: 2023-10-24 14:41:35
     */
    @OpenApi
    public Return getVoteUserTokenListPageByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.getInteger("pageIndex")-1;
        int recordSize = 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteUserTokenDao.getVoteUserTokenListPageByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteUserTokenDao.getVoteUserTokenCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
